﻿using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using System.Collections;
using LuaInterface;

public class TestCreateMesh : MonoBehaviour, IPointerEnterHandler, IPointerDownHandler{

	CanvasRenderer render;

	// Use this for initialization
	void Start () {
		render = GetComponent<CanvasRenderer> ();

		Debug.Log ("==============Start Lua test!===============");
		LuaState luaState = LuaClient.GetMainState ();
		LuaTable luaTable = luaState.GetTable ("TestTable");
		if (luaTable != null) {
			Debug.Log ("TestCreateMesh get luatable!");
			string name = luaTable.GetStringField ("name");
			Debug.Log ("TestCreateMesh get table field name: " + name);
		} else {
			Debug.Log ("TestCreateMesh can't get luatable!");
		}

		Button btn = GetComponent<Button> ();
		btn.onClick.AddListener (OnClick);
	}
	
	// Update is called once per frame
	void Update () {
		Mesh mesh = new Mesh();

		//Vector3[] vertices = new Vector3[]

		//mesh.vertices =

	}

	void LateUpdate() {
		
	}

	void OnRenderObject() {
		//Debug.Log ("OnRenderObject");

		GL.Begin(GL.LINES);

		/*
		for (int i = 0; i < 10; ++i)
		{
			GL.Color (Color.red);
			GL.Vertex(new Vector3(1.0,));

			GL.Vertex(GetPoint(i + 1));

		}
		*/

		GL.Color (Color.black);
		GL.Vertex(new Vector3(1.0f,-1.0f,1.0f));
		GL.Vertex (new Vector3 (3.0f, 5.0f, 1.0f));

		GL.End();
	}

	void OnPostRender() {
		//Debug.Log ("OnPostRender");

		GL.Begin(GL.LINES);

		/*
		for (int i = 0; i < 10; ++i)
		{
			GL.Color (Color.red);
			GL.Vertex(new Vector3(1.0,));

			GL.Vertex(GetPoint(i + 1));

		}
		*/

		GL.Color (Color.black);
		GL.Vertex(new Vector3(1.0f,-1.0f,1.0f));
		GL.Vertex (new Vector3 (3.0f, 5.0f, 1.0f));

		GL.End();
	}

	public void OnClick() {
		Debug.Log ("Btn Click!");

	}

	public void OnClickParam(GameObject clickObject) {
		Debug.Log("OnClickParam: " + clickObject.name);
	}

	public void OnClickParam2(GameObject clickObject, string str, int i){
	}

	public void OnPointerEnter (PointerEventData eventData) {
		Debug.Log("OnPointerEnter");
	}

	public void OnPointerDown (PointerEventData eventData) {
		Debug.Log("OnPointerDown: " + eventData.position);
		Debug.Log ("Input.mousePosition: " + Input.mousePosition);
	}
}
